define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");

    module.exports = {
        subnav: {
            handleQryCondClick: function (widget) {
                widget.get("grid").refresh();
            }
        },
        grid: {
            handleAppointClick: function (widget, data) {
                if (data.planItemGroup.checkInStatus.key == "Initial") {
                    var form = widget.get("form");
                    form.reset();
                    var str;
                    for(var i = 0; i<data.planItemGroup.signUpRoomTypeCounts.length;i++){
                    	if(i==0){
                    		str = data.planItemGroup.signUpRoomTypeCounts[i].roomType.name+"("+data.planItemGroup.signUpRoomTypeCounts[i].roomCount+")";
                    	}else{
                    		str+=","+data.planItemGroup.signUpRoomTypeCounts[i].roomType.name+"("+data.planItemGroup.signUpRoomTypeCounts[i].roomCount+")";
                    	}
                    }
                    form.setData({
                        planItemGroup: data.planItemGroup.pkPlanItemGroup,
                        destination: data.planItemGroup.planItem.destination.pkDestination,
                        destinationName: data.planItemGroup.planItem.destination.name,
                        startDate: data.planItemGroup.planItem.startDate,
                        endDate: data.planItemGroup.planItem.endDate,
                        roomType:str,
                        signUpNumber: data.signUpNumber,
                        checkInDate:data.planItemGroup.planItem.startDate,
                        checkOutDate:data.planItemGroup.planItem.endDate,
                    });
                    widget.toCard();
                  //隐藏预定按钮
                    $("div[el-data-component=rooms] .el-button").removeClass("hidden");
                } else {
                    Dialog.alert({
                        content: "该房间已预约！"
                    });
                }
            },
            handleDetailClick: function (widget,data) {
                var form = widget.get("form");
                form.reset();
                var roomTypeName = "";
                for(var i = 0; i < data.planItemGroup.signUpRoomTypeCounts.length; i++){
                	roomTypeName += data.planItemGroup.signUpRoomTypeCounts[i].roomType.name + "("+data.planItemGroup.signUpRoomTypeCounts[i].roomCount+"),"
                }
                form.setData({
                    planItemGroup: data.planItemGroup.pkPlanItemGroup,
                    destinationName: data.planItemGroup.planItem.destination.name,
                    startDate: data.planItemGroup.planItem.startDate,
                    endDate: data.planItemGroup.planItem.endDate,
                    signUpNumber: data.signUpNumber,
                    checkInDate:data.groupReserve.checkInDate,
                    checkOutDate:data.groupReserve.checkOutDate,
                    checkInType:data.groupReserve.checkInType.key,
                    allianceBuilding:data.groupReserve.allianceBuilding,
                    allianceRoom:data.groupReserve.allianceRoom,
                    description:data.groupReserve.description,
                    roomType:roomTypeName.substring(0,roomTypeName.length -1)
                });
                if(data.groupReserve.checkInType.key == "Organization"){
                	var buildingName = "";
                	for(var i = 0; i<data.groupReserve.roomRegisters.length;i++){
                		buildingName += data.groupReserve.roomRegisters[i].room.orgBuilding.name + ","
                	}
                	form.setValue("allianceBuilding", data.groupReserve.allianceBuilding);
                	var roomCodes = "";
                	var rrData = data.groupReserve.roomRegisters;
                	for(var i = 0; i < rrData.length; i++){
                		if(i == 0){
                			roomCodes += rrData[i].room.code;
                		}else{
                			roomCodes += "," + rrData[i].room.code;
                		}
                	}
                	form.setValue("rooms", roomCodes);
                }else{
                	form.setValue("allianceBuilding", data.groupReserve.allianceBuilding);
                	form.setValue("rooms", data.groupReserve.allianceRoom);
                }
                form.setDisabled(true);
                widget.toCard();
                //隐藏预定按钮
                $("div[el-data-component=rooms] .el-button").addClass("hidden");
            }
        },
        form: {
        	handleCheckInTypeChange: function (widget) {
        		var form = widget.get("form");
        		var value = form.getValue("checkInType");
        		if(value == "Alliance"){
        			widget.get("btn").element.addClass("hidden");
        			form.setDisabled("allianceBuilding",false);
        			// 入住联盟则，楼宇和房间为非必填，反之则反
        			$(".J-form textarea[name='allianceBuilding']").attr("data-rule-required", false);
        			$("div[el-data-component='allianceBuilding']").next().hide();
        			$(".J-form textarea[name='rooms']").attr("data-rule-required", false);
        			$("div[el-data-component='rooms']").next().hide();
        		}else{
        			widget.get("btn").element.removeClass("hidden");
        			form.setDisabled("allianceBuilding",true);
        			// 入住联盟则，楼宇和房间为非必填，反之则反
        			$(".J-form textarea[name='allianceBuilding']").attr("data-rule-required", true);
        			$("div[el-data-component='allianceBuilding']").next().show();
        			$(".J-form textarea[name='rooms']").attr("data-rule-required", true);
        			$("div[el-data-component='rooms']").next().show();
        		}
            },
            handleAppointBtnClick: function (widget) {
            	var model = widget.get("model");
            	var subnav = widget.get("subnav");
            	var form = widget.get("form");
            	//第0步，判断是否填写入住日期和退房日期
            	var checkInDate = form.getValue("checkInDate");
            	if(!checkInDate){
            		Dialog.alert({
            			content: "请填写入住日期"
            		});
            		return false;
            	}
            	var checkOutDate = form.getValue("checkOutDate");
            	if(!checkOutDate){
            		Dialog.alert({
            			content: "请填写退房日期"
            		});
            		return false;
            	}
            	subnav.setValue("appointTime",{
            		start: checkInDate,
            		end: checkOutDate
            	})
            	//第一步 根据目的地查询楼宇
            	widget.service.queryBuildingByDes({
            		pkDestination: widget.get("form").getValue("destination")
            	},function(buildings){
            		//根据楼宇查询房型
            		subnav.setData("buildings",buildings);
            		if(buildings.length != 0){
            			widget.service.queryRoomTypeByBuilding({
                			orgBuilding: subnav.getValue("buildings")
                		},function(roomTypes){
                			subnav.setData("roomTypes",roomTypes);
                			if(roomTypes.length != 0){
                				var appointTime = subnav.getValue("appointTime");
                    			widget.service.queryRoom({
                    				"orgBuilding.pkOrgBuilding": subnav.getValue("buildings"),
                    				"roomType.pkRoomType": subnav.getValue("roomTypes"),
                    				startDate: appointTime.start,
                    				endDate: appointTime.end,
                    				fetchProperties:"room.pkRoom,room.code,room.status.name,checkOutDate,isWillGo,isAppoint"
                    			},function(rooms){
                    				var ret = widget.store.procRoomDatas(widget,rooms);
                    				model.datas = ret;
                    				widget.renderPartial(".J-content");
                    				widget.renderPartial(".J-selectedData");
                    				//默认显示净房的数据
                    				widget.$(".J-status.clear").click();
                    				/*widget.$(".J-status").removeClass("onClick");
                    				widget.$(".J-status.clear").addClass("onClick");*/
                    			});
                        		widget.toAppoint();
                			}else{
                				Dialog.alert({
                    				content: "该楼宇没有房型!"
                    			});
                			}
                		});
            		}else{
            			Dialog.alert({
            				content: "该目的地没有楼宇!"
            			});
            		}
            	});
            },
            handleSaveBtnClick: function (widget) {
                var form = widget.get("form");
                var data = form.getData();
                if(data.checkInDate > data.checkOutDate){
                	Dialog.alert({
        				content: "入住日期不能晚于退房日期！"
        			});
                	return false;
                }
                var cParams = {
                    planItemGroup: data.planItemGroup,
                    "planItemGroup.servicePoint": widget.get("subnav").getValue("servicePoint"),
                    reserveDate: data.reserveDate,
                    checkInDate: data.checkInDate,
                    checkOutDate: data.checkOutDate,
                    operator: data.operator,
                    checkInType: data.checkInType,
                    allianceBuilding: data.allianceBuilding,
                    description: data.description,
                    version: data.version,
                    roomRegisters: []
                };
                if(data.checkInType != "Organization"){
                	//直属结构
                	cParams.allianceRoom = data.rooms;
                }else{
                	var rrList= [];
                	var model = widget.get("model");
                	var selectedDatas = model.selectedDatas;
                	for(var i in selectedDatas){
                		var buildings = selectedDatas[i].buildings || {};
                		for(var j in buildings){
                			var rooms = buildings[j].rooms || {};
                			for(var k in rooms){
                				if(rooms[k].pkRoomRegister){
                					//有pkRoomRegister
                					rrList.push({
                						pkRoomRegister:rooms[k].pkRoomRegister
                					})
                				}else{
                					rrList.push({
                						room:rooms[k].pkRoom,
                						version: 0
                					})
                				}
                			}
                		}
                	}
                }
                widget.service.save(aw.customParam({
                	groupReserve: cParams,
                	rrList: rrList
                }),function(ret){
                    widget.get("grid").refresh();
                    widget.toList();
                    //清空已选择的房间
                    widget.get("model").selectedDatas = {};
                });
            },
            handleCancelBtnClick: function (widget) {
                widget.toList();
                widget.get("model").selectedDatas = {};
            }
        },
        appointComp: {
            handleBuildChange: function (widget) {
            	var model = widget.get("model");
            	var subnav = widget.get("subnav");
        		//根据楼宇查询房型
        		widget.service.queryRoomTypeByBuilding({
        			orgBuilding: subnav.getValue("buildings")
        		},function(roomTypes){
        			subnav.setData("roomTypes",roomTypes);
        			subnav.show("roomTypes");
        			if(roomTypes.length != 0){
        				var appointTime = subnav.getValue("appointTime");
            			widget.service.queryRoom({
            				"orgBuilding.pkOrgBuilding": subnav.getValue("buildings"),
            				"roomType.pkRoomType": subnav.getValue("roomTypes"),
            				startDate: appointTime.start,
            				endDate: appointTime.end,
            				fetchProperties:"room.pkRoom,room.code,room.status.name,checkOutDate,isWillGo,isAppoint"
            			},function(rooms){
            				var ret = widget.store.procRoomDatas(widget,rooms);
            				model.datas = ret;
            				widget.renderPartial(".J-content");
            				widget.renderPartial(".J-selectedData");
            				//默认显示净房的数据
            				widget.$(".J-status.clear").click();
            				/*widget.$(".J-status").removeClass("onClick");
            				widget.$(".J-status.clear").addClass("onClick");*/
            			});
        			}else{
        				Dialog.alert({
            				content: "该楼宇没有房型!"
            			});
        			}
        		});
            },
            handleRoomTypeChange: function (widget) {
            	var model = widget.get("model");
            	var subnav = widget.get("subnav");
    			var appointTime = subnav.getValue("appointTime");
    			widget.service.queryRoom({
    				"orgBuilding.pkOrgBuilding": subnav.getValue("buildings"),
    				"roomType.pkRoomType": subnav.getValue("roomTypes"),
    				startDate: appointTime.start,
    				endDate: appointTime.end,
    				fetchProperties:"room.pkRoom,room.code,room.status.name,checkOutDate,isWillGo,isAppoint"
    			},function(rooms){
    				var ret = widget.store.procRoomDatas(widget,rooms);
    				model.datas = ret;
    				widget.renderPartial(".J-content");
    				widget.renderPartial(".J-selectedData");
    				//默认显示净房的数据
    				widget.$(".J-status.clear").click();
    				/*widget.$(".J-status").removeClass("onClick");
    				widget.$(".J-status.clear").addClass("onClick");*/
    			});
            },
            handleCheckInDateChange: function (widget) {
            	var model = widget.get("model");
            	var subnav = widget.get("subnav");
    			var appointTime = subnav.getValue("appointTime");
    			widget.service.queryRoom({
    				"orgBuilding.pkOrgBuilding": subnav.getValue("buildings"),
    				"roomType.pkRoomType": subnav.getValue("roomTypes"),
    				startDate: appointTime.start,
    				endDate: appointTime.end,
    				fetchProperties:"room.pkRoom,room.code,room.status.name,checkOutDate"
    			},function(rooms){
    				var ret = widget.store.procRoomDatas(widget,rooms);
    				model.datas = ret;
    				widget.renderPartial(".J-content");
    				widget.renderPartial(".J-selectedData");
    				//默认显示净房的数据
    				widget.$(".J-status.clear").click();
    				/*widget.$(".J-status").removeClass("onClick");
    				widget.$(".J-status.clear").addClass("onClick");*/
    			});
            },
            handleConfirmBtnClick: function (widget) {
            	widget.toCard();
            	var ret = "";
            	var buildName = "";
            	var model = widget.get("model");
            	var selectedDatas = model.selectedDatas;
            	
            	//去除重复楼宇
            	var buildArr = [];
            	var count = 0;
            	for(var i in selectedDatas){
            		var buildings = selectedDatas[i].buildings || {};
            		for(var j in buildings){
            			var falg = true;
            			if(count==0){
            				buildArr[0]=buildings[j].text|| {};
            				count++;
            			}else{
            				for(var k =0; k<buildArr.length;k++){
            					if(buildings[j].text == buildArr[k]){
            						falg = false;
            					}
            				}
            				if(falg){
            					buildArr[count]=buildings[j].text|| {};
            					count++;
            				}
            			}
            		}
            	}
            	
            	for(var k =0; k<buildArr.length;k++){
            		buildName += buildArr[k] + ",";
            	}
            	
            	//回写表单中的房间字段
            	for(var i in selectedDatas){
            		var buildings = selectedDatas[i].buildings || {};
            		for(var j in buildings){
            			var rooms = buildings[j].rooms || {};
            			for(var k in rooms){
            				ret += rooms[k].code + ",";
            			}
            		}
            	}
            	widget.get("form").setValue("rooms",ret.substring(0,ret.length -1));
            	widget.get("form").setDisabled("allianceBuilding",true);
            	if(ret.length != 0){
            		widget.get("form").setValue("allianceBuilding", buildName.substring(0,buildName.length -1));
            	}
            },
            handleCancelBtnClick: function (widget) {
            	widget.toCard();
            	widget.get("model").selectedDatas = {};
            }
        }
    };
});